package main.java.DMDE.persistence.conversion;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import main.java.DMDE.model.Diagram;
import main.java.DMDE.styling.StyleConstants;
import main.java.common.IXmlFileManager;
import main.java.common.XmlFileManager;

import org.apache.commons.collections.map.HashedMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ERMGraphToDMGraphXmlAdapter {
	
	private IXmlFileManager xmlFileManager = new XmlFileManager();
	
	public void convertXml(String ermGraphXmlRoute, String dmGraphXmlRoute, String diagramName) throws Exception {
	    	String ermRoute =  ermGraphXmlRoute + "/" + diagramName + "-rep";
	    	String dmRoute =  dmGraphXmlRoute + "/" + diagramName + "-rep";
		Document ermDocument = xmlFileManager.read(ermRoute);
		Document dmDocument = this.xmlFileManager.createDocument();
		Element graphModel = dmDocument.createElement("mxGraphModel");
		Element root = dmDocument.createElement("root");
		graphModel.appendChild(root);
		dmDocument.appendChild(graphModel);
		NodeList graphElement = ermDocument.getDocumentElement().getElementsByTagName("root").item(0).getChildNodes();
		if (graphElement != null) for (int i = 0; i < graphElement.getLength(); i++) {
			Node node = graphElement.item(i);
			if (node == null || !(node instanceof Element))
				continue;
			
			Element cellElement = (Element) node;
			String rawId = cellElement.getAttribute("id");
			if (rawId.equals("0") || rawId.equals("1")) {
				Element cell = dmDocument.createElement("mxCell");
				copyAttribute(cellElement, cell, "id");
				if (rawId.equals("1")) {
					copyAttribute(cellElement, cell, "parent");
				}
				root.appendChild(cell);
				continue;
			}
			if (!rawId.startsWith("Entity") && !rawId.startsWith("Relationship"))
				continue;
			Element mxCell = dmDocument.createElement("mxCell");
			copyAttribute(cellElement, mxCell, "vertex");
			copyAttribute(cellElement, mxCell, "parent");
			copyAttribute(cellElement, mxCell, "vertex");
			copyAttribute(cellElement, mxCell, "value");
			mxCell.setAttribute("style", StyleConstants.CLASS_STYLE);
			if (rawId.startsWith("Entity"))
				mxCell.setAttribute("id", rawId.replace("Entity", "Class"));
			else if (rawId.startsWith("RelationshipConnector")) {
				continue;
//				mxCell.setAttribute("id", rawId.replace("RelationshipConnector", "Relationship"));
//				mxCell.setAttribute("source", cellElement.getAttribute("target").replace("Entity", "Class"));
//				mxCell.setAttribute("source", cellElement.getAttribute("source").replace("Relationship", "Class"));
			}
			else if (rawId.startsWith("Relationship")) {
				mxCell.setAttribute("id", rawId.replace("Relationship", "Class"));	
			}
			Element geometry = (Element) cellElement.getChildNodes().item(0);
			Element newGeometry = dmDocument.createElement("mxGeometry");
			copyAttribute(geometry, newGeometry, "as");
			copyAttribute(geometry, newGeometry, "x");
			copyAttribute(geometry, newGeometry, "y");
			copyAttribute(geometry, newGeometry, "height");
			copyAttribute(geometry, newGeometry, "width");
			mxCell.appendChild(newGeometry);
			root.appendChild(mxCell);
		}
		this.xmlFileManager.write(dmDocument, dmRoute);
	}
	
	private void copyAttribute(Element origin, Element destination, String attributeName) {
		destination.setAttribute(attributeName, origin.getAttribute(attributeName));
	}
}
